import kafka from "node-rdkafka";
import { conf } from "../confParse.js";
import { EventCode, eventHandler } from "../event/eventHandler.js";

async function createRespProducer(conf:conf):Promise<kafka.Producer>{
    let producer =new kafka.Producer({
        'metadata.broker.list':conf.brokerList,
        'dr_cb':true
    },{
        'acks':1,
    })
    producer.connect()
    producer.on("event.error",(err)=>{
        eventHandler.handle(EventCode.CONNECT_KAFKA_ERROR,"producer occur err ",new Error(err.message))
        throw err
    })
    producer.setPollInterval(100)
    await new Promise<void>((resolve)=>{
        producer.on("ready",()=>{
            resolve()
        })
    })
    return producer
}

export {createRespProducer}